package cpr.view;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.w3c.dom.Node;

import service.ServiciosListaPrecios;
import xml.SerializadorListaPrecios;
import xml.XmlUtil;

import bean.vo.ListaPreciosVO;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("serial")
public class CargarListaProveedor extends javax.swing.JFrame implements ActionListener {

	private JTextField txtInputFile;
	private JButton btnSelectFile;
	private JButton btnImportSelected;
	private File selectedFile;
	
	public CargarListaProveedor() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("Cargar lista de precios");
			this.setPreferredSize(new java.awt.Dimension(700, 150));
			
			
			txtInputFile = new JTextField();
			txtInputFile.setBounds(38, 6, 406, 20);
			txtInputFile.setEditable(false);
			getContentPane().add(txtInputFile);
			txtInputFile.setColumns(50);
			
			JButton btnSelectFile = new JButton("...");
			btnSelectFile.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					JFileChooser fileChooser = new JFileChooser();
					FileNameExtensionFilter filter = new FileNameExtensionFilter("XML", "xml");
					fileChooser.setFileFilter(filter);
					
					int seleccion = fileChooser.showOpenDialog(txtInputFile);
					if (seleccion == JFileChooser.APPROVE_OPTION)
					{
						selectedFile = fileChooser.getSelectedFile();
						txtInputFile.setText(selectedFile.getAbsolutePath());
						btnImportSelected.setEnabled(true);
					}
				}
			});
			btnSelectFile.setBounds(449, 5, 45, 23);
			getContentPane().add(btnSelectFile);
			
			btnImportSelected = new JButton("Import");
			btnImportSelected.addActionListener(this);
			btnImportSelected.setEnabled(false);
			btnImportSelected.setBounds(499, 5, 80, 23);
			getContentPane().add(btnImportSelected);
			
			
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
JButton source = (JButton) arg0.getSource();
		if (source == btnImportSelected)
		{

			try {
				Node nodoLista = XmlUtil.getRootNode(txtInputFile.getText());
				ListaPreciosVO lista = SerializadorListaPrecios.deserializar(nodoLista);
				ServiciosListaPrecios.crearListaPrecios(lista, lista.getNumeroListaReemplazada());
				
				JOptionPane.showMessageDialog(this, "Lista importada con exito", "Resultado", JOptionPane.INFORMATION_MESSAGE);
			} catch (Exception e) {
				JOptionPane.showMessageDialog(this, e.getMessage(), "", JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	
}
